<template>
  <div class="login">
    <div class="back">
      <div class="x" @click="closeLogin">
        <i class="vbestui-iconfont icon-close"></i>
      </div>
      <div class="son">
        <i class="iconfont icon-Phone phone"></i>
      </div>
    </div>
    <div class="main">
      <el-form :rules="formRules" ref="loginForm" :model="formObj">
        <el-form-item prop="phone">
          <el-input v-model="formObj.phone" placeholder="输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="formObj.password"
            placeholder="输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="form_center">
          <el-button type="danger" @click="SubmitLogin()">登录</el-button>
        </el-form-item>
        <el-form-item class="form_center">
          <el-link type="primary">注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { _login } from '../../network/user'
export default {
  data () {
    return {
      formRules: {
        //验证的数据
        phone: [
          { message: '请输入手机号', required: true, trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8]\d{9}$/,
            message: '手机号格式错误',
            trigger: 'blur'
          }
        ],
        password: [{ message: '请输入密码', required: true, trigger: 'blur' }]
      },
      formObj: {
        //v-model的数据
        phone: '',
        password: ''
      }
    }
  },
  methods: {
    closeLogin () {
      //触发父组件方法 关闭
      this.$parent.hiddenLogin()
    },
    SubmitLogin () {
      //登录方法
      // 表单组件的验证方法
      this.$refs.loginForm.validate(valid => {
        console.log(valid) //valid true 表单验证通过  false 某项未通过
        if (valid) {
          // 调用登录接口
          _login(this.formObj.phone, this.formObj.password).then(res => {
            console.log(res)
            if (res.data.code != 200) {
              this.$message.error('密码错误')
            }
            // 储存信息
            this.$store.commit('addUser', res.data)
            localStorage.setItem('cookie', res.data.cookie)
            localStorage.setItem('avatar', res.data.profile.avatarUrl)
            localStorage.setItem('uid', res.data.profile.userId)
            // 重置整个表单
            this.$refs.loginForm.resetFields()
            // 触发父组件的方法 关闭登录
            this.$parent.hiddenLogin()
          })
        }
      })
    }
  }
}
</script>
<style lang="less">
.login {
  width: 400px;
  height: 560px;
  background: #fafafa;
  z-index: 999;
  position: absolute;
  top: 100px;
  left: 50%;
  margin-left: -200px;
}
.back {
  width: 100%;
  height: 30%;
  position: relative;
}
.x {
  width: 26px;
  height: 26px;
  float: right;
  margin-right: 5px;
  line-height: 26px;
  cursor: pointer;
  text-align: center;
  margin-top: 5px;
  .icon-close {
    font-size: 24px;
  }
}
.back .son {
  width: 58px;
  height: 102px;
  position: absolute;
  left: 0;
  right: 20px;
  top: 80px;
  bottom: 0;
  margin: auto;
}
.phone {
  color: red;
  font-size: 64px !important;
}
.main {
  width: 60%;
  margin: 20px auto;
}
.form_center {
  display: flex;
  justify-content: center;
}
</style>
